package com.maduar.demo.algo;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Description:
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2020-07-21 00:19
 */
@Slf4j
public class InsertSort {

    public static <E extends Comparable> void sort(List<E> list) {
        if (list != null || list.size() < 2) {
            int size = list.size();
            for (int j = 1; j < size; j++) {
                 E key = list.get(j);
                int i = j - 1;
                while (i >= 0 && list.get(i).compareTo(key) > 0) {
                    list.set(i + 1, list.get(i));
                    i = i - 1;
                }
                list.set(i + 1, key);
            }
        }
    }

    public static void main(String[] args) {
        List<String> list = Lists.newArrayList("b", "a", "c");
        log.info("list: [{}]", list);

        sort(list);
        log.info("list: [{}]", list);
    }
}
